package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysRoleDao;
import ysq.xyg.sys.model.po.SysRole;
import ysq.xyg.sys.model.po.SysRoleMenu;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysRoleVo;
import ysq.xyg.sys.service.SysRoleMenuService;
import ysq.xyg.sys.service.SysRoleService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 系统管理-角色service实现
 *
 * @author shengQiang yu
 * @date 2019-3-28 11:50:53
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {

    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    private SysRole sysRole;

    @Autowired
    public void setBaseDao(SysRoleDao sysRoleDao) {
        this.baseDao = sysRoleDao;
    }


    /**
     * 分页查询
     *
     * @param sysRoleVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    public ReturnEntity page(SysRoleVo sysRoleVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<SysRole> list = acceptList(sysRoleVo);
        PageInfo<SysRole> page=new PageInfo<SysRole>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param sysRoleVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    public ReturnEntity addOrUpdate(SysRoleVo sysRoleVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysRoleVo.getId())){
            sysRole=new SysRole(currentUser);
            isUpate=false;
        }else{
            sysRole=selectByPrimaryKey(sysRoleVo.getId());
            if(ObjectUtil.isNull(sysRole)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysRole.setUpdateTime(new Date());
            sysRole.setUpdateUser(currentUser.getId());
        }
            sysRole.setRoleName(sysRoleVo.getRoleName());
            sysRole.setSort(sysRoleVo.getSort());
            sysRole.setStatus(sysRoleVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysRole);
        }else{
            num=insert(sysRole);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }else{
            if(isUpate){
                sysRoleMenuService.delete(new SysRoleMenu(sysRole.getId()));
                addRoleMenu(currentUser,sysRoleVo.getMenuIds(),sysRole.getId());
            }else{
                addRoleMenu(currentUser,sysRoleVo.getMenuIds(),sysRole.getId());
            }
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysRole sysRole=selectByPrimaryKey(id);
        if(sysRole!=null){
            sysRole.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            sysRole.setUpdateTime(new Date());
            sysRole.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(sysRole);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param sysRoleVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-3-28 11:50:53
     */
    public List<SysRole> acceptList(SysRoleVo sysRoleVo){
        Example example = new Example(SysRole.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysRoleVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysRoleVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(sysRoleVo.getRoleName())){
            criteria.andLike("roleName","%"+sysRoleVo.getRoleName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return sysRoleDao.selectByExample(example);
    }

    /**
     * 批量添加角色菜单
     * @param currentUser 当前用户
     * @param menuIds 菜单集合
     * @param roleId 角色id
     * @author shengQiang yu
     * @date 2018-10-15 10:33
     */
    private void addRoleMenu(SysUser currentUser,String menuIds,String roleId){
        if(StringUtil.isNotEmpty(menuIds)){
            String[] menuIdList=menuIds.split(",");
            for (String menuId:menuIdList){
                sysRoleMenuService.insert(new SysRoleMenu(currentUser,roleId,menuId));
            }
        }
    }
}
